<script setup>

import {
    CaretBottom,
    Crop,
    EditPen, Goods,
    Management,
    Promotion,
    SwitchButton,
    User,
    UserFilled
} from '@element-plus/icons-vue'
import avatar from '@/assets/default.png'
import { useUserInfoStore } from "@/stores/userInfo";
import { useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import { onMounted } from 'vue'

const userInfoStore = useUserInfoStore();
onMounted(() => {
    const infoStr = localStorage.getItem('userInfo')
    if (infoStr) {
        try {
            const info = JSON.parse(infoStr)
            // 确保是学生身份
            if (info.role === 'student') {
                userInfoStore.setUserInfo(info)
            }
        } catch (e) {
            // ignore parse error
        }
    }
})

const router = useRouter();
const handleCommand = (command) => {
    if (command === 'logout') {
        ElMessageBox.confirm(
            '你确认要退出吗？',
            '温馨提示',
            {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning'
            }
        ).then(
            async () => {
                // 清除本地存储
                localStorage.removeItem('token')
                localStorage.removeItem('userRole')
                localStorage.removeItem('userInfo')
                // clear data in pinia
                userInfoStore.removeUserInfo()
                ElMessage.success("退出成功")
                await router.push('/login')
            }
        )
    } else if (command === 'info') {
        router.push('/student/info')
    } else if (command === 'resetPassword') {
        router.push('/student/resetPassword')
    }
}
</script>

<template>
    <el-container class="layout-container">
        <!-- 左侧菜单 -->
        <el-aside width="200px">
            <div class="el-aside__logo">
                <h2>SUSE学生活动平台</h2>
            </div>
            <el-menu active-text-color="#409eff" background-color="#6B71D7" text-color="#4a5568" router>
                <el-menu-item index="/student/activity">
                    <el-icon>
                        <Promotion />
                    </el-icon>
                    <span>活动列表</span>
                </el-menu-item>
                <el-menu-item index="/student/enrolled">
                    <el-icon>
                        <Goods />
                    </el-icon>
                    <span>我的报名</span>
                </el-menu-item>
                <el-sub-menu>
                    <template #title>
                        <el-icon>
                            <UserFilled />
                        </el-icon>
                        <span>个人中心</span>
                    </template>
                    <el-menu-item index="/student/info">
                        <el-icon>
                            <User />
                        </el-icon>
                        <span>基本资料</span>
                    </el-menu-item>
                    <el-menu-item index="/student/resetPassword">
                        <el-icon>
                            <EditPen />
                        </el-icon>
                        <span>修改密码</span>
                    </el-menu-item>
                </el-sub-menu>
            </el-menu>
        </el-aside>
        <!-- 右侧主区域 -->
        <el-container>
            <!-- 头部区域 -->
            <el-header>
                <div>欢迎：<strong>{{ userInfoStore.userInfo.name || userInfoStore.userInfo.username }}</strong>同学回来</div>
                <el-dropdown placement="bottom-end" @command="handleCommand">
                    <span class="el-dropdown__box">
                        <el-avatar :src="userInfoStore.userInfo.avatar || avatar" :size="40" fit="cover" />
                        <el-icon>
                            <CaretBottom />
                        </el-icon>
                    </span>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <el-dropdown-item command="info" :icon="User">基本资料</el-dropdown-item>
                            <el-dropdown-item command="resetPassword" :icon="EditPen">修改密码</el-dropdown-item>
                            <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
            </el-header>
            <!-- 中间区域 -->
            <el-main>
                <router-view />
            </el-main>
            <!-- 底部区域 -->
            <el-footer>SUSE学生活动报名系统 ©2025 </el-footer>
        </el-container>
    </el-container>
</template>

<style lang="scss" scoped>
.layout-container {
    height: 100vh;

    .el-aside {
        background: linear-gradient(180deg, #667eea 0%, #764ba2 100%);
        position: relative;
        overflow: hidden;

        &::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="grain" width="100" height="100" patternUnits="userSpaceOnUse"><circle cx="20" cy="20" r="1" fill="rgba(255,255,255,0.1)"/><circle cx="80" cy="40" r="0.5" fill="rgba(255,255,255,0.05)"/><circle cx="40" cy="80" r="1.5" fill="rgba(255,255,255,0.08)"/></pattern></defs><rect width="100" height="100" fill="url(%23grain)"/></svg>');
            opacity: 0.3;
        }

        &__logo {
            height: 80px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: rgba(255, 255, 255, 0.15);
            margin: 16px;
            border-radius: 16px;
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.2);
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
            position: relative;
            z-index: 1;

            h2 {
                color: white;
                font-size: 16px;
                font-weight: 700;
                margin: 0;
                text-align: center;
                line-height: 1.2;
                text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
                letter-spacing: 0.5px;
            }
        }

        .el-menu {
            border-right: none;
            background: transparent;
            position: relative;
            z-index: 1;

            .el-menu-item {
                margin: 6px 12px;
                border-radius: 12px;
                transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
                position: relative;
                overflow: hidden;

                &::before {
                    content: '';
                    position: absolute;
                    top: 0;
                    left: -100%;
                    width: 100%;
                    height: 100%;
                    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
                    transition: left 0.5s;
                }

                &:hover {
                    background: rgba(255, 255, 255, 0.1);
                    color: white;
                    transform: translateX(4px);
                    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);

                    &::before {
                        left: 100%;
                    }
                }

                &.is-active {
                    background: rgba(255, 255, 255, 0.2);
                    color: white;
                    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
                    transform: translateX(2px);

                    &::after {
                        content: '';
                        position: absolute;
                        right: 8px;
                        top: 50%;
                        transform: translateY(-50%);
                        width: 4px;
                        height: 20px;
                        background: white;
                        border-radius: 2px;
                        box-shadow: 0 0 8px rgba(255, 255, 255, 0.5);
                    }
                }

                .el-icon {
                    margin-right: 8px;
                    font-size: 18px;
                }

                span {
                    font-weight: 500;
                }
            }

            .el-sub-menu {
                margin: 6px 12px;
                border-radius: 12px;

                .el-sub-menu__title {
                    border-radius: 12px;
                    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
                    position: relative;
                    overflow: hidden;

                    &::before {
                        content: '';
                        position: absolute;
                        top: 0;
                        left: -100%;
                        width: 100%;
                        height: 100%;
                        background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
                        transition: left 0.5s;
                    }

                    &:hover {
                        background: rgba(255, 255, 255, 0.1);
                        color: white;
                        transform: translateX(4px);
                        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);

                        &::before {
                            left: 100%;
                        }
                    }
                }

                .el-menu-item {
                    margin: 4px 0;
                    padding-left: 40px;
                    background: rgba(255, 255, 255, 0.05);
                    border-left: 2px solid rgba(255, 255, 255, 0.3);
                    color: pink !important;
                    transition: all 0.3s ease;

                    &:hover {
                        background: rgba(255, 255, 255, 0.15);
                        border-left-color: pink;
                        color: pink !important;
                        transform: translateX(2px);
                    }

                    &.is-active {
                        background: rgba(255, 255, 255, 0.25);
                        border-left-color: pink;
                        color: pink !important;
                        transform: translateX(4px);
                        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);

                        &::after {
                            content: '';
                            position: absolute;
                            right: 8px;
                            top: 50%;
                            transform: translateY(-50%);
                            width: 4px;
                            height: 20px;
                            background: pink;
                            border-radius: 2px;
                        }
                    }

                    span {
                        color: pink !important;
                        font-weight: 600;
                        transition: all 0.3s ease;
                        text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
                    }

                    .el-icon {
                        color: pink !important;
                        transition: all 0.3s ease;
                        filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.3));
                    }
                }
            }
        }
    }

    .el-header {
        background: linear-gradient(135deg, #ffffff 0%, #f8fafc 100%);
        display: flex;
        align-items: center;
        justify-content: space-between;
        box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
        border-bottom: 1px solid rgba(102, 126, 234, 0.1);
        position: relative;
        z-index: 10;

        &::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 2px;
            background: linear-gradient(90deg, #667eea, #764ba2, #667eea);
            background-size: 200% 100%;
            animation: gradientShift 3s ease-in-out infinite;
        }

        @keyframes gradientShift {

            0%,
            100% {
                background-position: 0% 50%;
            }

            50% {
                background-position: 100% 50%;
            }
        }

        .el-dropdown__box {
            display: flex;
            align-items: center;
            cursor: pointer;
            padding: 12px 16px;
            border-radius: 12px;
            transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            background: rgba(102, 126, 234, 0.05);
            border: 1px solid rgba(102, 126, 234, 0.1);
            position: relative;
            overflow: hidden;

            &::before {
                content: '';
                position: absolute;
                top: 0;
                left: -100%;
                width: 100%;
                height: 100%;
                background: linear-gradient(90deg, transparent, rgba(102, 126, 234, 0.1), transparent);
                transition: left 0.5s;
            }

            &:hover {
                background: rgba(102, 126, 234, 0.1);
                border-color: rgba(102, 126, 234, 0.2);
                transform: translateY(-1px);
                box-shadow: 0 4px 12px rgba(102, 126, 234, 0.15);

                &::before {
                    left: 100%;
                }
            }

            .el-avatar {
                margin-right: 12px;
                border: 3px solid rgba(255, 255, 255, 0.8);
                transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
                box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);

                &:hover {
                    border-color: #667eea;
                    transform: scale(1.1);
                    box-shadow: 0 4px 16px rgba(102, 126, 234, 0.3);
                }
            }

            .el-icon {
                color: #667eea;
                margin-left: 8px;
                font-size: 16px;
                transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
            }

            &:hover .el-icon {
                transform: rotate(180deg);
                color: #5a67d8;
            }

            &:active,
            &:focus {
                outline: none;
            }
        }
    }

    .el-main {
        background: linear-gradient(135deg, #f8fafc 0%, #ffffff 100%);
        padding: 24px;
        position: relative;
        overflow: hidden;

        &::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background:
                radial-gradient(circle at 20% 20%, rgba(102, 126, 234, 0.03) 0%, transparent 50%),
                radial-gradient(circle at 80% 80%, rgba(118, 75, 162, 0.03) 0%, transparent 50%),
                radial-gradient(circle at 40% 60%, rgba(102, 126, 234, 0.02) 0%, transparent 50%);
            pointer-events: none;
        }

        >* {
            position: relative;
            z-index: 1;
        }
    }

    .el-footer {
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 14px;
        color: #64748b;
        background: linear-gradient(135deg, #ffffff 0%, #f1f5f9 100%);
        border-top: 1px solid rgba(102, 126, 234, 0.1);
        position: relative;

        &::before {
            content: '';
            position: absolute;
            top: 0;
            left: 50%;
            transform: translateX(-50%);
            width: 60px;
            height: 2px;
            background: linear-gradient(90deg, #667eea, #764ba2);
            border-radius: 1px;
        }
    }
}
</style>
